package com.nulldev.util.web.WebSocketAPI.demo;

import java.net.URI;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.web.WebSocketAPI.WebSocket;
import com.nulldev.util.web.WebSocketAPI.WebSocketEventHandler;
import com.nulldev.util.web.WebSocketAPI.WebSocketException;
import com.nulldev.util.web.WebSocketAPI.WebSocketMessage;

public class WebSocketAPIDemo {

	public static void main(String[] args) throws Exception {
		final WebSocket ws = new WebSocket(new URI("ws://demos.kaazing.com/echo"));
		ws.setEventHandler(new WebSocketEventHandler() {

			@Override
			public void onOpen() {
				JVM.println("Socket opened.");
				ws.send("Hello world!");
				ws.close();
			}

			@Override
			public void onMessage(WebSocketMessage message) {
				JVM.println("message -> " + message);
			}

			@Override
			public void onLogMessage(String msg) {
				JVM.println("log: " + msg);
			}

			@Override
			public void onError(WebSocketException e) {
				JVM.errorln(e);
			}

			@Override
			public void onClose() {
				JVM.println("Socket closed.");
			}
		});
		ws.connect();
	}
}
